package test;

import java.awt.List;
import java.io.IOException;
import java.rmi.AlreadyBoundException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;

import message.Message;
import message.MessageType;
import node.ActualNode;
import node.Node;

/**
 * To test the multi threaded.
 * @author POOJA
 *
 */
public class testMultithread {
  /**
   * The node.
   */
  private static Node my_node;


  /**
   * @param args
   * @throws AlreadyBoundException 
   * @throws IOException 
   */
  public static void main(String[] the_args) throws AlreadyBoundException, IOException {

    final String host = the_args[0];
    final int port = Integer.parseInt(the_args[1]);
    final String registry_name = the_args[2];

    try {
      final Registry bootstrap = LocateRegistry.getRegistry(host, port);
      final Registry registry = (Registry) bootstrap.lookup(registry_name);


      Node my_node = new ActualNode(2, 3, registry);
      registry.bind("node_id_" + 2, UnicastRemoteObject.exportObject(my_node, 0));
      Node n = (Node) registry.lookup("node_id_2");


      System.out.println("This is node id " + n.getMyID());

      Message request = new Message(n.getMyID(), "Pooja", "Katti" ,MessageType.PUT_VALUE);
      Message request1 = new Message(n.getMyID(), "pa", "ka" ,MessageType.PUT_VALUE);
      Message request2 = new Message(n.getMyID(), "po0", "ja" ,MessageType.PUT_VALUE);
      System.out.println("sending message.............  +" + request.getValue() );
     ArrayList<Message> reg = new ArrayList<Message>(); 
     
     reg.add(request);
     reg.add(request1);
     reg.add(request2);
     
     for(int i = 0; i < reg.size() ; i++){
        
//     n.getResponse(reg.get(i));
      }
     
//     request = new Message(n.getMyID(), "Tina",null, MessageType.GET_VALUE);
//      Message response = (Message)n.getResponse(request);
//      if(response != null) {
//        System.out.println(response.getValue());            
//      } else {
        System.out.println("Doesn't find the value of this key!");
//      }
//    } catch (RemoteException e) {
     
//      e.printStackTrace();
    } catch (NotBoundException e) { 
      
      e.printStackTrace();
    } 
  }
}
